package com.kcsoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kcsoft.mapper.TeacherMapper;
import com.kcsoft.pojo.Classes;
import com.kcsoft.pojo.Message;
import com.kcsoft.pojo.PageBean;
import com.kcsoft.pojo.User;
import com.kcsoft.service.TeacherService;
import com.kcsoft.utils.Md5Util;
import com.kcsoft.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.management.relation.Role;
import java.util.List;
import java.util.Map;

@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Value("${default_pwd}")
    private String defaultPwd;

    @Override
    public void makeClass(String name) {
        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer userId = (Integer) claims.get("id");
        teacherMapper.makeClass(name, userId);
    }

    @Override
    public void delClasses(Integer id) {
        teacherMapper.delClasses(id);
    }

    @Override
    public void addStus(List<Integer> ids, Integer classId) {
        teacherMapper.addStus(ids, classId);
    }

    @Override
    public void setModel(Integer id, Integer value) {
        teacherMapper.setModel(id, value);
    }

    @Override
    public void resetPwd(Integer id) {
        String password = Md5Util.genPwd(defaultPwd);
        teacherMapper.resetPwd(id, password);
    }

    @Override
    public PageBean<User> listUser(Integer pageNum, Integer pageSize, Integer classId, String stuId) {

        PageBean<User> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> claims = ThreadLocalUtil.get();

        Integer role = (Integer) claims.get("role");

        List<User> al = teacherMapper.listUser(classId, stuId, role);

        Page<User> u = (Page<User>) al;

        pb.setTotal(u.getTotal());

        pb.setItems(u.getResult());

        return pb;

    }

    @Override
    public List<Classes> listClasses() {

        Map<String, Object> claims = ThreadLocalUtil.get();
        Integer id = (Integer) claims.get("id");
        List<Classes> al = teacherMapper.listClasses(id);

        return al;

    }

    @Override
    public PageBean<Message> listMsg(Integer pageNum, Integer pageSize) {

        PageBean<Message> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        Map<String, Object> claims = ThreadLocalUtil.get();

        Integer role = (Integer) claims.get("role");

        List<Message> al = teacherMapper.listMsg(role);

        Page<Message> m = (Page<Message>) al;

        pb.setTotal(m.getTotal());

        pb.setItems(m.getResult());

        return pb;

    }

    @Override
    public void delStus(List<Integer> ids) {
        teacherMapper.delStus(ids);
    }

    @Override
    public PageBean<User> listStus(Integer pageNum, Integer pageSize, Integer classId) {

        PageBean<User> pb = new PageBean<>();

        PageHelper.startPage(pageNum, pageSize);

        List<User> al = teacherMapper.listStus(classId);

        Page<User> u = (Page<User>) al;

        pb.setTotal(u.getTotal());

        pb.setItems(u.getResult());

        return pb;

    }

    @Override
    public void addUsers(List<User> users) {
        teacherMapper.addUsers(users);
    }

}
